﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for SurveyQuestionPopUp.
    /// </summary>
    public class SurveyQuestionPopUp : System.Web.UI.Page
    {
        #region VARIABLES
        protected System.Web.UI.WebControls.TextBox Ctl_Question;
        protected System.Web.UI.WebControls.DropDownList Dp_QuestionType;
        protected System.Web.UI.WebControls.TextBox Ctl_Sort;
        protected System.Web.UI.WebControls.CheckBox Ctl_Enable;
        protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.DropDownList ddlFlowType;
        protected System.Web.UI.WebControls.Label lblTitle;
        protected System.Web.UI.WebControls.Button Btn_Delete;
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            string ItemIDStr = Request["ItemID"];
            //string StateStr = Request["State"];

            if (ItemIDStr != "0")
            {
                this.ItemID = Convert.ToInt32(ItemIDStr);
                this.ddlFlowType.Enabled = false;
                this.Dp_QuestionType.Enabled = false;
            }
            else
            {
                this.ItemID = 0;
                this.ddlFlowType.Enabled = true;
                this.Dp_QuestionType.Enabled = true;
            }

            if (!this.IsPostBack)
            {
                this.Page.Response.Expires = 0;
                InitFlowType();

                this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                this.Btn_Delete.Attributes["onclick"] = "javascript:OnDelete();";

                //WebUtility.BuildVehicleType(this.Dp_VehicleType,BEGDC.CC.BL.Common.DicType.AlcatelCar);

                if (this.ItemID > 0)
                {
                    this.Btn_Delete.Visible = true;
                    InitForm();
                }

                if (ItemIDStr != "0")
                {
                    this.lblTitle.Text = "Modify Survey Question (修改调查问题)";
                }
                else
                {
                    this.lblTitle.Text = "Add Survey Question (添加调查问题)";
                    this.Btn_Delete.Visible = false;
                }
            }
        }

        #region ItemID
        protected int ItemID
        {
            get { return (int)this.ViewState["QuestionItemID"]; }
            set { this.ViewState["QuestionItemID"] = value; }
        }
        //		protected string State
        //		{
        //			get{ return this.ViewState["AlcatelCarState"].ToString(); }
        //			set{ this.ViewState["AlcatelCarState"] = value; }
        //		}
        #endregion

        #region Init Flow Type

        private void InitQuestionType(string dicType)
        {
            this.Dp_QuestionType.Items.Clear();
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            IList list = utility.GetAllDicItem(dicType);
            foreach (BEGDC.CC.Entity.DicItem item in list)
            {
                this.Dp_QuestionType.Items.Add(new System.Web.UI.WebControls.ListItem(item.Description, item.DicItemKey));
            }

        }

        private void InitFlowType()
        {
            //init flow type

            if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin) && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
            {
                this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Domestic & Overseas Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.CV));
                this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Reception In Shanghai", BEGDC.CC.BL.Workflow.WorkflowTypes.CR));
                this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.OCR));
                this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.OCV));
            }
            else
            {
                if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin))
                {
                    this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Domestic & Overseas Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.CV));
                    this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Customer Reception In Shanghai", BEGDC.CC.BL.Workflow.WorkflowTypes.CR));

                }
                else if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
                {
                    this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Reception", BEGDC.CC.BL.Workflow.WorkflowTypes.OCR));
                    //this.ddlFlowType.Items.Add(new System.Web.UI.WebControls.ListItem("Overseas Customer Visiting", BEGDC.CC.BL.Workflow.WorkflowTypes.OCV));


                }
            }

            this.ddlFlowType.SelectedIndex = 0;

            ddlFlowType_SelectedIndexChanged(null, null);


        }
        #endregion
        #region InitForm
        private void InitForm()
        {
            ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
            BEGDC.CC.Entity.SurveyQuestion Question = CRService.GetSurveyQuestion(this.ItemID);
            this.Ctl_Question.Text = Question.Question;
            this.Dp_QuestionType.SelectedIndex = Dp_QuestionType.Items.IndexOf(Dp_QuestionType.Items.FindByValue(Question.Type));
            this.ddlFlowType.SelectedValue = Question.ApplicationType;
            this.Ctl_Sort.Text = Question.Sort.ToString();
            this.Ctl_Enable.Checked = Question.IsEnable;
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Save.Click += new System.EventHandler(this.Btn_Save_Click);
            this.Btn_Delete.Click += new System.EventHandler(this.Btn_Delete_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.SurveyQuestion Qst = null;
                if (this.ItemID > 0)
                {
                    Qst = CRService.GetSurveyQuestion(this.ItemID);
                }
                else
                {
                    Qst = new BEGDC.CC.Entity.SurveyQuestion();
                }
                Qst.Question = this.Ctl_Question.Text;
                Qst.Type = this.Dp_QuestionType.SelectedValue.ToString();
                Qst.ApplicationType = this.ddlFlowType.SelectedValue;
                string Sort = this.Ctl_Sort.Text;
                if (Sort != null || Sort != "")
                {
                    Qst.Sort = Convert.ToInt32(this.Ctl_Sort.Text);
                }
                Qst.IsEnable = this.Ctl_Enable.Checked;
                CRService.UpdateSurveyQuestion(Qst, false);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.SurveyQuestion Question = null;
                if (this.ItemID > 0)
                {
                    Question = CRService.GetSurveyQuestion(this.ItemID);
                    CRService.UpdateSurveyQuestion(Question, true);
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            Response.Write("<script>window.close();window.returnValue='reload';</script>");
        }

        private void ddlFlowType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string flowtype = ddlFlowType.SelectedValue;
            if (flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.CR || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.CV)
            {
                this.InitQuestionType(BEGDC.CC.BL.Common.DicType.QuestionType);
            }
            else if (flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.OCV || flowtype == BEGDC.CC.BL.Workflow.WorkflowTypes.OCR)
            {
                this.InitQuestionType(BEGDC.CC.BL.Common.DicType.OCOQuestionType);
            }

        }
    }
}
